package cn.com.pc.content.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;


@Component
public class JDBCUtils {

    private static String url;
    private static String user;
    private static String password;
    private static String driver;

    @Value("${oldPcp.datasource.url:jdbc:mysql://rm-wz9b2dp0r7a8m273j.mysql.rds.aliyuncs.com:3306/pcp}")
    private void setUrl(String value) {
        url = value;
    }

    @Value("${oldPcp.datasource.user:pcp}")
    private void setUser(String value) {
        user = value;
    }

    @Value("${oldPcp.datasource.password:34OMVUU38#}")
    private void setPassword(String value) {
        password = value;
    }

    @Value(value = "${oldPcp.datasource.driver:com.mysql.cj.jdbc.Driver}")
    private void setdriver(String value) {
        driver = value;
    }

    /**
     * 获取连接对象
     *
     * @return 连接对象
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return DriverManager.getConnection(url, user, password);
    }

    /**
     * 释放资源
     *
     * @param stmt
     * @param conn
     */

    public static void close(ResultSet rs, Statement stmt,Connection conn) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void close(Statement stmt, Connection conn) {
        close(null , stmt, conn);
    }
}
